package nige.capture;

import java.util.Vector;

public class Triangles {
	public Points sommet1 = null;
	public Points sommet2 = null;
	public Points sommet3 = null;
	
	public Triangles(Points som1, Points som2, Points som3) {
		Points somTmp = null;
		if(som1.xValue*10000+som1.yValue*100+som1.zValue > 
			som2.xValue*10000+som2.yValue*100+som2.zValue) {
			somTmp = som1;
			som1 = som2;
			som2 = somTmp;
		}
		if(som2.xValue*10000+som2.yValue*100+som2.zValue > 
			som3.xValue*10000+som3.yValue*100+som3.zValue) {
			somTmp = som2;
			som2 = som3;
			som3 = somTmp;
		}
		if(som1.xValue*10000+som1.yValue*100+som1.zValue > 
		som2.xValue*10000+som2.yValue*100+som2.zValue) {
		somTmp = som1;
		som1 = som2;
		som2 = somTmp;
	}
		sommet1 = som1;
		sommet2 = som2;
		sommet3 = som3;
	}
	
	public int getBlue() {
		int col = 
			(sommet1.colour.getBlue()+sommet2.colour.getBlue()+
					sommet3.colour.getBlue())/3;
		return col;
	}
	public int getRed() {
		int col = 
			(sommet1.colour.getRed()+sommet2.colour.getRed()+
					sommet3.colour.getRed())/3;
		return col;
	}
	public int getGreen() {
		int col = 
			(sommet1.colour.getGreen()+sommet2.colour.getGreen()+
					sommet3.colour.getGreen())/3;
		return col;
	}
	
	public boolean equals(Triangles tri) {
		if(
				sommet1.equals(tri.sommet1)&& 
				sommet2.equals(tri.sommet2)&& 
				sommet3.equals(tri.sommet3) 
				) return true;
		if(
				sommet1.equals(tri.sommet1)&& 
				sommet2.equals(tri.sommet3)&& 
				sommet3.equals(tri.sommet2) 
				) return true;
		if(
				sommet1.equals(tri.sommet2)&& 
				sommet2.equals(tri.sommet1)&& 
				sommet3.equals(tri.sommet3) 
				) return true;
		if(
				sommet1.equals(tri.sommet2)&& 
				sommet2.equals(tri.sommet3)&& 
				sommet3.equals(tri.sommet1) 
				) return true;
		if(
				sommet1.equals(tri.sommet3)&& 
				sommet2.equals(tri.sommet1)&& 
				sommet3.equals(tri.sommet2) 
				) return true;
		if(
				sommet1.equals(tri.sommet3)&& 
				sommet2.equals(tri.sommet2)&& 
				sommet3.equals(tri.sommet1) 
				) return true;
		return false;
	}
	
	public boolean isComplete(Triangles triag) {
		int nbSomCom = 0;
		if(
				triag.sommet1.equals(sommet1) ||
				triag.sommet1.equals(sommet2) ||
				triag.sommet1.equals(sommet3)
				) {
			nbSomCom++;
		}
		if(
				triag.sommet2.equals(sommet1) ||
				triag.sommet2.equals(sommet2) ||
				triag.sommet2.equals(sommet3)
				) {
			nbSomCom++;
		}
		if(
				triag.sommet3.equals(sommet1) ||
				triag.sommet3.equals(sommet2) ||
				triag.sommet3.equals(sommet3)
				) {
			nbSomCom++;
		}
		return (nbSomCom==2);
	}
	
	public String toString() {
		return "S1 : "+sommet1+"\n"+"S2 : "+sommet2+"\n"+"S3 : "+sommet3+"\n";
	}
	
	public boolean becomeDelaunay(Triangles triag) {
		boolean ret = false;
		Points som1 = null;
		Points som2 = null;
		Points som3 = null;
		Points som4 = null;
		if(triag.sommet1.equals(sommet1)) {
			if(som1 == null) {
				som1 = sommet1;
			}
			else {
				som2 = sommet1;
			}
		}
		if(triag.sommet1.equals(sommet2)) {
			if(som1 == null) {
				som1 = sommet2;
			}
			else {
				som2 = sommet2;
			}
		}
		if(triag.sommet1.equals(sommet3)) {
			if(som1 == null) {
				som1 = sommet3;
			}
			else {
				som2 = sommet3;
			}
		}
		if(triag.sommet2.equals(sommet1)) {
			if(som1 == null) {
				som1 = sommet1;
			}
			else {
				som2 = sommet1;
			}
		}
		if(triag.sommet2.equals(sommet2)) {
			if(som1 == null) {
				som1 = sommet2;
			}
			else {
				som2 = sommet2;
			}
		}
		if(triag.sommet2.equals(sommet3)) {
			if(som1 == null) {
				som1 = sommet3;
			}
			else {
				som2 = sommet3;
			}
		}
		if(triag.sommet3.equals(sommet1)) {
			if(som1 == null) {
				som1 = sommet1;
			}
			else {
				som2 = sommet1;
			}
		}
		if(triag.sommet3.equals(sommet2)) {
			if(som1 == null) {
				som1 = sommet2;
			}
			else {
				som2 = sommet2;
			}
		}
		if(triag.sommet3.equals(sommet3)) {
			if(som1 == null) {
				som1 = sommet3;
			}
			else {
				som2 = sommet3;
			}
		}
		/*
		System.out.println();
		System.out.println(sommet1+" --- "+sommet2+" --- "+sommet3);
		System.out.println(som1+" + "+som2);
		*/
		if(!sommet1.equals(som1) && !sommet1.equals(som2)) {
			som3 = sommet1;
		}
		if(!sommet2.equals(som1) && !sommet2.equals(som2)) {
			som3 = sommet2;
		}
		if(!sommet3.equals(som1) && !sommet3.equals(som2)) {
			som3 = sommet3;
		}
		
		if(!triag.sommet1.equals(som1) && !triag.sommet1.equals(som2)) {
			som4 = triag.sommet1;
		}
		if(!triag.sommet2.equals(som1) && !triag.sommet2.equals(som2)) {
			som4 = triag.sommet2;
		}
		if(!triag.sommet3.equals(som1) && !triag.sommet3.equals(som2)) {
			som4 = triag.sommet3;
		}
		return inversion(som1,som2,som3,som4);
	}
	
	private boolean inversion(Points som1,Points som2,Points som3,Points som4) {
		// System.out.println(som1+" + "+som2+" + "+som3+" + "+som4);
		//TODO: PARFOIS LES SOMMET SONT EGAUX !! => NULL
		if(som1==null || som2==null || som3==null || som4==null) {
			return false;
		}
		if(som1.distance(som2) > som3.distance(som4)) {
			Triangles tria1 = new Triangles(som3,som4,som1);
			Triangles tria2 = new Triangles(som3,som4,som2);
			if(tria1.isInAbsTriag(som1) || tria2.isInAbsTriag(som1) ||
					tria1.isInAbsTriag(som2) || tria2.isInAbsTriag(som2)
					) {
				return false;
			}
			Maillage.triangles.add(tria1);
			Maillage.triangles.add(tria2);
			return true;
		}
		return false;
	}
	
	public boolean isInTriag(Points pts) {
		float xInters = 0;
		float yInters = 0;
		if(sommet2.xValue == sommet3.xValue) {
			xInters = sommet2.xValue;
			yInters = (sommet2.yValue+sommet3.yValue)/2;
		}
		else {
			float aInt = 
				((float)sommet1.yValue-(float)pts.yValue)/
				((float)sommet1.xValue-(float)pts.xValue);
			float bInt = (float)sommet1.yValue - aInt*(float)sommet1.xValue;
			float a = 
				((float)sommet2.yValue-(float)sommet3.yValue)/
				((float)sommet2.xValue-(float)sommet3.xValue);
			float b = (float)sommet2.yValue - a*(float)sommet2.xValue;
			if((aInt-a) == 0) {
				xInters = sommet1.xValue;
			}
			else {
				xInters = (b-bInt)/(aInt-a);
			}
			yInters = a*xInters + b;
		}
		float xMax = Math.max(sommet2.xValue,sommet3.xValue);
		float xMin = Math.min(sommet2.xValue,sommet3.xValue);
		float yMax = Math.max(sommet2.yValue,sommet3.yValue);
		float yMin = Math.min(sommet2.yValue,sommet3.yValue);
		
		float xMaxInt = Math.max(xInters,sommet1.xValue);
		float xMinInt = Math.min(xInters,sommet1.xValue);
		float yMaxInt = Math.max(yInters,sommet1.yValue);
		float yMinInt = Math.min(yInters,sommet1.yValue);
		
		if(
				xInters <= xMax && xInters >= xMin &&
				yInters <= yMax && yInters >= yMin &&
				pts.xValue <= xMaxInt && pts.xValue >= xMinInt &&
				pts.yValue <= yMaxInt && pts.yValue >= yMinInt
				) {
			return true;
		}
		
		return false;
	}
	
	public boolean isInAbsTriag(Points pts) {
		float xInters = 0;
		float yInters = 0;
		if(sommet2.xValue == sommet3.xValue) {
			xInters = sommet2.xValue;
			yInters = (sommet2.yValue+sommet3.yValue)/2;
		}
		else {
			float aInt = 
				((float)sommet1.yValue-(float)pts.yValue)/
				((float)sommet1.xValue-(float)pts.xValue);
			float bInt = (float)sommet1.yValue - aInt*(float)sommet1.xValue;
			float a = 
				((float)sommet2.yValue-(float)sommet3.yValue)/
				((float)sommet2.xValue-(float)sommet3.xValue);
			float b = (float)sommet2.yValue - a*(float)sommet2.xValue;
			if((aInt-a) == 0) {
				xInters = sommet1.xValue;
			}
			else {
				xInters = (b-bInt)/(aInt-a);
			}
			yInters = a*xInters + b;
		}
		float xMax = Math.max(sommet2.xValue,sommet3.xValue);
		float xMin = Math.min(sommet2.xValue,sommet3.xValue);
		float yMax = Math.max(sommet2.yValue,sommet3.yValue);
		float yMin = Math.min(sommet2.yValue,sommet3.yValue);
		
		float xMaxInt = Math.max(xInters,sommet1.xValue);
		float xMinInt = Math.min(xInters,sommet1.xValue);
		float yMaxInt = Math.max(yInters,sommet1.yValue);
		float yMinInt = Math.min(yInters,sommet1.yValue);
		
		if(
				xInters < xMax && xInters > xMin &&
				yInters < yMax && yInters > yMin &&
				pts.xValue < xMaxInt && pts.xValue > xMinInt &&
				pts.yValue < yMaxInt && pts.yValue > yMinInt
				) {
			return true;
		}
		
		return false;
	}
	
}
